package com.budwk.app.enterprise.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.common.page.PageUtil;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import com.budwk.app.enterprise.models.EnterpriseShareholders;
import com.budwk.app.enterprise.services.EnterpriseshareholdersService;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;
import org.nutz.lang.Strings;
import com.budwk.starter.common.openapi.annotation.*;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletRequest;

@IocBean
@At("/platform/enterprise/enterprise/enterprise/shareholders")
@ApiDefinition(tag = "企业股东信息")
@Slf4j
public class EnterpriseshareholdersController{
    @Inject
    private EnterpriseshareholdersService   wxEnterpriseShareholdersService;

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询企业股东信息")
    @ApiFormParams(
        value = {
            @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
            @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
            @ApiFormParam(name = "pageOrderName", example = "createdAt", description = "排序字段"),
            @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
        },
        implementation = EnterpriseShareholders.class
    )
    @ApiResponses(
    implementation = EnterpriseShareholders.class
    )
    @SaCheckPermission("enterprise.enterprise.wx.enterprise.shareholders")
    public Result<?> list(@Param("..")EnterpriseShareholders bean,@Param("pageNo") int pageNo, @Param("pageSize") int pageSize, @Param("pageOrderName") String pageOrderName, @Param("pageOrderBy") String pageOrderBy) {
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(pageOrderName) && Strings.isNotBlank(pageOrderBy)) {
            cnd.orderBy(pageOrderName, PageUtil.getOrder(pageOrderBy));
        }
        
        if (bean.getNameShareholders() !=null && Strings.isNotBlank(bean.getNameShareholders().toString())) {
            cnd.and("nameShareholders","=",bean.getNameShareholders());
        }
        
        if (bean.getTypeShares() !=null && Strings.isNotBlank(bean.getTypeShares().toString())) {
            cnd.and("typeShares","=",bean.getTypeShares());
        }
        
        if (bean.getNumberSharesHeld() !=null && Strings.isNotBlank(bean.getNumberSharesHeld().toString())) {
            cnd.and("numberSharesHeld","=",bean.getNumberSharesHeld());
        }
        
        if (bean.getFinalBeneficiaryShares() !=null && Strings.isNotBlank(bean.getFinalBeneficiaryShares().toString())) {
            cnd.and("finalBeneficiaryShares","=",bean.getFinalBeneficiaryShares());
        }
        
        if (bean.getProportionTotalShareCapital() !=null && Strings.isNotBlank(bean.getProportionTotalShareCapital().toString())) {
            cnd.and("proportionTotalShareCapital","=",bean.getProportionTotalShareCapital());
        }
        
        if (bean.getShareholdingRatio() !=null && Strings.isNotBlank(bean.getShareholdingRatio().toString())) {
            cnd.and("shareholdingRatio","=",bean.getShareholdingRatio());
        }
        
        if (bean.getAmountCapitalSubscribed() !=null && Strings.isNotBlank(bean.getAmountCapitalSubscribed().toString())) {
            cnd.and("amountCapitalSubscribed","=",bean.getAmountCapitalSubscribed());
        }
        
        if (bean.getDateSubscription() !=null && Strings.isNotBlank(bean.getDateSubscription().toString())) {
            cnd.and("dateSubscription","=",bean.getDateSubscription());
        }
        
        if (bean.getAmountCapitalPaid() !=null && Strings.isNotBlank(bean.getAmountCapitalPaid().toString())) {
            cnd.and("amountCapitalPaid","=",bean.getAmountCapitalPaid());
        }
        
        if (bean.getDateFirstShareholding() !=null && Strings.isNotBlank(bean.getDateFirstShareholding().toString())) {
            cnd.and("dateFirstShareholding","=",bean.getDateFirstShareholding());
        }
        
        if (bean.getRelatedProductsOrganizations() !=null && Strings.isNotBlank(bean.getRelatedProductsOrganizations().toString())) {
            cnd.and("relatedProductsOrganizations","=",bean.getRelatedProductsOrganizations());
        }
        
        return Result.data(  wxEnterpriseShareholdersService.listPage(pageNo, pageSize, cnd));
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "新增企业股东信息")
    @ApiFormParams(
        implementation = EnterpriseShareholders.class
    )
    @ApiResponses
    @SLog(tag = "新增企业股东信息", value = "${args[0].id}")
    @SaCheckPermission("enterprise.enterprise.wx.enterprise.shareholders.create")
    public Result<?> create(@Param("..")EnterpriseShareholders   wxEnterpriseShareholders, HttpServletRequest req) {
          wxEnterpriseShareholders.setCreatedBy(SecurityUtil.getUserId());
          wxEnterpriseShareholdersService.insert(  wxEnterpriseShareholders);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "修改企业股东信息")
    @ApiFormParams(
        implementation = EnterpriseShareholders.class
    )
    @ApiResponses
    @SLog(tag = "修改企业股东信息", value = "${args[0].id}")
    @SaCheckPermission("enterprise.enterprise.wx.enterprise.shareholders.update")
    public Result<?> update(@Param("..")EnterpriseShareholders   wxEnterpriseShareholders, HttpServletRequest req) {
          wxEnterpriseShareholders.setUpdatedBy(SecurityUtil.getUserId());
          wxEnterpriseShareholdersService.updateIgnoreNull(  wxEnterpriseShareholders);
        return Result.success();
    }

    @At("/get")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取企业股东信息")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SaCheckPermission("enterprise.enterprise.wx.enterprise.shareholders")
    public Result<?> getData(@Param("id")String id, HttpServletRequest req) {
        EnterpriseShareholders   wxEnterpriseShareholders =   wxEnterpriseShareholdersService.fetch(id);
        if (  wxEnterpriseShareholders == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        return Result.data(  wxEnterpriseShareholders);
    }

    @At("/delete")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除企业股东信息")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SLog(tag = "删除企业股东信息", value = "${args[0]}")
    @SaCheckPermission("enterprise.enterprise.wx.enterprise.shareholders.delete")
    public Result<?> delete(@Param("id")String id, HttpServletRequest req) {
        EnterpriseShareholders   wxEnterpriseShareholders =   wxEnterpriseShareholdersService.fetch(id);
        if (  wxEnterpriseShareholders == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
          wxEnterpriseShareholdersService.delete(id);
        req.setAttribute("_slog_msg",   wxEnterpriseShareholders.getId());
        return Result.success();
    }
}
